package com.polaris.live.common

import com.polaris.live.common.util.resettableLazy
import com.polaris.live.common.util.resettableManager
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertTrue
import org.junit.Test

/**
 * LazyUtilsTest
 *
 * @author Created by lucas on 2023/10/26 15:33
 */
class LazyUtilsTest {

    @Test
    fun testResettableLazy() {
        val s = Something()
        val x1 = s.x
        val y1 = s.y
        val z1 = s.z

        assertEquals(x1, s.x)
        assertEquals(y1, s.y)
        assertEquals(z1, s.z)

        s.seed++ // without reset nothing should change

        assertTrue(x1 === s.x)
        assertTrue(y1 === s.y)
        assertTrue(z1 === s.z)

        s.lazyMgr.reset()

        s.seed++ // because of reset the values should change

        val x2 = s.x
        val y2 = s.y
        val z2 = s.z

        assertEquals(x2, s.x)
        assertEquals(y2, s.y)
        assertEquals(z2, s.z)

        assertNotEquals(x1, x2)
        assertNotEquals(y1, y2)
        assertNotEquals(z1, z2)

        s.seed++ // but without reset, nothing should change

        assertTrue(x2 === s.x)
        assertTrue(y2 === s.y)
        assertTrue(z2 === s.z)
    }
}

class Something {

    var seed = 1
    val lazyMgr = resettableManager()

    val x: String by resettableLazy(lazyMgr) { "x $seed" }
    val y: String by resettableLazy(lazyMgr) { "y $seed" }
    val z: String by resettableLazy(lazyMgr) { "z $x $y" }

}